package com.oa.controller;

import com.oa.back.ResponseBack;
import com.oa.common.annotion.Interceptor;
import com.oa.common.enums.CodeStatus;
import com.oa.handler.AuthenticationTonken;
import com.oa.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.Objects;

@Controller
@RequestMapping()
public class LoginController extends BaseController {

    private static final Logger logger= LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value = "login/index.html",method = RequestMethod.GET)
    public String index(){
        return "/admin/login/index";
    }


    @RequestMapping(value = "login/submit",method = RequestMethod.POST)
    @Interceptor(actionName = "登录的接口")
    @ResponseBody
    public ResponseBack<String> submit(String username, String password, String code){
        AuthenticationTonken tonken= new AuthenticationTonken(username, password, code);
        Subject subject=SecurityUtils.getSubject();
            try{
            subject.login(tonken);
            return ResponseBack.success(null);
            }catch (RuntimeException ex){
                logger.error("登录接口的异常:"+ex);
                try{
                    return ResponseBack.fial(Objects.requireNonNull(CodeStatus.getResponseStatus(ex.getMessage())));
                }catch (RuntimeException e){
                    return ResponseBack.fial(CodeStatus.SYSTEM_FAIL.getResponseStatus());
                }
            }
    }

    @RequestMapping(value = "uploads/{filename}/{year}/{date}/{name:.+}",method = RequestMethod.GET)
    public void index(HttpServletRequest request, HttpServletResponse response,
                      @PathVariable String filename, @PathVariable String year,
                      @PathVariable String date, @PathVariable String name){
        try {
            String path=filename+"/"+year+"/"+date+"/"+name;
            path="c://uploads/"+path;
            File file = new File(path);
            // 判断文件是否存在如果不存在就返回默认图标
            if (!(file.exists() && file.canRead())) {
                file = new File(request.getSession().getServletContext().getRealPath("/") +
                        "/images/head_default.png");
            }
            FileInputStream inputStream = null;
            inputStream = new FileInputStream(file);

            byte[] data = new byte[(int) file.length()];
            inputStream.read(data);
            inputStream.close();
            response.setContentType("image/png,jpg");
            OutputStream stream = response.getOutputStream();
            stream.write(data);
            stream.flush();
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
